package cn.hwadee.muying.service;

import cn.hwadee.muying.commons.base.BaseService;
import cn.hwadee.muying.commons.constant.ConstantRole;
import cn.hwadee.muying.commons.page.MyPage;
import cn.hwadee.muying.commons.ztree.ZTreeNode;
import cn.hwadee.muying.entity.Permission;
import cn.hwadee.muying.entity.Role;
import cn.hwadee.muying.entity.User;
import cn.hwadee.muying.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Tony
 * @since 2018-04-17
 */
@Service
public class RoleService extends BaseService<RoleMapper, Role> {


    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionService permService;

    //@Autowired
    //private SellerService sellerService;

    public MyPage<Role> list(MyPage<Role> page, User cUser) throws Exception{
        if(cUser.getRid().equals(ConstantRole.ROLE_SUPER)){
            //超级管理员获取系统内使用的角色
            page.setRecords(getCommonRoles());
        }else {
            //机构管理员获取机构内使用的角色
            page.setRecords(getByOrgId(cUser.getOrgId()));
        }
        return page;
    }

    public List<Role> getByOrgId(Long orgId) throws Exception{
        return roleMapper.getByOrgId(orgId);
    }

    /**
     * 获取所有非机构的角色
     * @return
     * @throws Exception
     */
    public List<Role> getCommonRoles() throws Exception{
        return roleMapper.getCommonRoles();
    }

    public void insertRole(Role role, User cUser) throws Exception{
        role.setOrgId(cUser.getOrgId());
        super.insert(role);
    }

    /**
     * 获取权限树，并设置已选权限
     * @param checkIds
     * @return
     * @throws Exception
     */
    public List<ZTreeNode> selectPermTree(String checkIds, User cUser) throws Exception{

        List<Permission> perms = permService.list();

        if(cUser != null){
            String ids = "";
            if(cUser.getRid() != null){
                if(cUser.getRid().equals(ConstantRole.ROLE_SUPER)){
                    for(Permission perm:perms){
                        ids += perm.getId() + ",";
                    }
                }else {
                    Role role = this.selectById(cUser.getRid());
                    ids = role.getPerms();
                }
            }
            List<String> idArray;
            List<String> checkIdArray;
            if(ids == null){
                idArray = new ArrayList<>();
            }else {
                idArray = Arrays.asList(ids.split(","));
            }
            if(checkIds == null){
                checkIdArray = new ArrayList<>();
            }else {
                checkIdArray = Arrays.asList(checkIds.split(","));
            }
            List<ZTreeNode> tree = new ArrayList<>();
            for(Permission perm:perms){
                ZTreeNode treeNode = new ZTreeNode();
                treeNode.setId(perm.getId() + "");
                treeNode.setName(perm.getName());
                treeNode.setPid(perm.getPid() + "");
                if(!checkIdArray.isEmpty() && checkIdArray.contains(perm.getId()+"")){
                    treeNode.setChecked(true);
                }
                if((!idArray.isEmpty() && idArray.contains(perm.getId()+""))){
                    tree.add(treeNode);
                }
            }
            return tree;
        }else {
            return null;
        }
    }


    /**
     * 获取卖家树，并设置已选权限
     * @param checkIds
     * @return
     * @throws Exception
     */
    /*
    public List<ZTreeNode> selectSellerTree(String checkIds, User cUser) throws Exception{

        List<Seller> sellers = new ArrayList<>();

        if(cUser != null){
            String ids = null;
            List<String> idArray = new ArrayList<>();
            List<String> checkIdArray;
            if(cUser.getRid() != null){
                Role role = this.selectById(cUser.getRid());
                ids = role.getSeller();
                if(StringUtils.isEmpty(ids)){
                    if(role.getId().equals(ConstantRole.ROLE_ORGANIZATION_ADMIN)){
                        sellers = sellerService.listSellersByBiz(cUser.getBizId());
                        for(Seller seller:sellers){
                            idArray.add(seller.getId()+"");
                        }
                    }else if(role.getId().equals(ConstantRole.ROLE_SUPER)){
                        sellers = sellerService.listAllSellers();
                        for(Seller seller:sellers){
                            idArray.add(seller.getId()+"");
                        }
                    }else {
                        return new ArrayList<>();
                    }
                }else {
                    sellers = sellerService.listSellersByIds(ids);
                    idArray = Arrays.asList(ids.split(","));
                }
            }
            if(checkIds == null){
                checkIdArray = new ArrayList<>();
            }else {
                checkIdArray = Arrays.asList(checkIds.split(","));
            }
            List<ZTreeNode> tree = new ArrayList<>();
            for(Seller seller : sellers){
                ZTreeNode treeNode = new ZTreeNode();
                treeNode.setId(seller.getId() + "");
                treeNode.setName(seller.getSellerName());
                treeNode.setPid("0");
                if(!checkIdArray.isEmpty() && checkIdArray.contains(seller.getId()+"")){
                    treeNode.setChecked(true);
                }
                if(!idArray.isEmpty() && idArray.contains(seller.getId()+"")){
                    tree.add(treeNode);
                }
            }
            return tree;
        }else {
            return null;
        }
    }
    */
}
